<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>副业</title>
</head>
<style>
    div{
        height: 50px;
        flex: auto;
        display: block;
        margin: 10px;
        font-size: 30px;
        font-style:oblique;
        align-content: center;
        
    }
    table{
        border-style:solid;
        flex: content;
    }
    body{
        flex: content;
    }

</style>
<body>
    <div>
        <td><label for="">添加博客或者修改</label></td>
    </div>
    <table>
        <tr>
            <td><label for="">标题:</label></td>
            <td><input type="text" placeholder="请输入" name="biaoti"></td>
        </tr>
        <tr>
            <td><label for="">摘要:</label></td>
            <td><input type="text" placeholder="请输入" name="zaiyao"></td>
        </tr>
        <tr>
            <td><label for="">内容:</label></td>
            <td><input type="text" placeholder="请输入" name="neirong"></td>
        </tr>
        <tr>
            <td><label for="">分类:</label></td>
            <td><input type="text" placeholder="请输入" name="fenglei"></td>
        </tr>
        <tr>
            <td><label for="">作者:</label></td>
            <td><input type="text" placeholder="请输入" name="zuozhe"></td>
        </tr>
        <tr id="sa11">
            <td>
                <input type="button" value="保存" onclick="save()">
                <input type="button" value="取消" onclick="cencal()">
            </td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config/baseUrl.js"></script>
    <script src="./js/app.js"></script>
    <script>

        $(function () {
            let id = getUrlParam('id');
            id = id || 0;
            console.log(id);
            asByid(id).then(res => {
                let a = res.data;
                let name = $('[name=biaoti]').val(a.biaoti);
                $('[name=zaiyao]').val(a.zaiyao);
                $('[name=neirong]').val(a.neirong);
                $('[name=fenglei]').val(a.fenglei);
                $('[name=zuozhe]').val(a.zuozhe);
            })
        })
        function getUrlParam(name) {

            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象

            var r = window.location.search.substr(1).match(reg); //匹配目标参数

            if (r != null) return unescape(r[2]); return null; //返回参数值

        }
        //保存进行修改或者新增
        function save() {
            let id = getUrlParam('id');
            console.log(id);
            let obj = {
                biaoti: $('[name=biaoti]').val(),
                zaiyao: $('[name=zaiyao]').val(),
                neirong: $('[name=neirong]').val(),
                fenglei: $('[name=fenglei]').val(),
                zuozhe: $('[name=zuozhe]').val(),
            }
            console.log(obj);
            if (id) {
                console.log(10);
                asupdate(id, obj).then(res => {
                    console.log(res);
                    location.href = './index.html'
                })
            } else {
                console.log(20);
                let params = {
                    biaoti: obj.biaoti,
                    zaiyao: obj.zaiyao,
                    neirong: obj.neirong,
                    fenglei: obj.fenglei,
                    zuozhe: obj.zuozhe,
                }
                asadd(params).then(res => {
                    location.href = './index.html'
                })
            }
        }
        //返回
        function cencal() {
            location.href = './index.html'
        }
    </script>
</body>

</html>